#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "interface.h"
#include "debug.h"
#include <pthread.h>


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


#define buflen 1024*10


void* test_send_fun(void* arg){
	int i;
	int len;
	char* buf;
	int fd;
	fd = open("../text", O_RDONLY);
	assert(fd!=-1);

	for(i=0; i<110000; i++){
		debug_info("send data!\n");
		len = (rand() % (buflen)) + 1;  //assert len !=0 
		
		buf = malloc(len);
		len = read(fd, buf, len);
		assert(len!=-1);
		send_data_sync(1,buf, len);
		free(buf);
	}

	return NULL;
}



void usage(char* argv){
	printf("Usage:%s -i id  \n", argv);
}

void send_cb(msg_t* msg){
	assert(msg->buf !=NULL);
	free(msg->buf);
} 


int main(int argc, char* argv[]){
	
	int d = 10;
	pthread_t pid_send;
	pthread_t pid_recv;
	int rc;
	int ch;
	int server_id;

	   opterr = 0;
	   while((ch = getopt(argc, argv, "i:d")) != -1)
	   switch(ch)
	   {
	      case 'i':
			 server_id = atoi(optarg);
			 printf("server id:%d\n", server_id);
			 break;
	      case 'd':
	         printf("run in daemon\n");
			 break;
	      default:
	         usage(argv[0]);
			 exit(0);
	   }

	   sys_init(0,send_cb);
	   rc = pthread_create(&pid_send, NULL, test_send_fun, NULL);
	   assert(rc!=-1);

	   pthread_join(pid_send, NULL);
	   sys_exit();


}
